/**
 * author:yhb
 * 登录过滤器类，防止用户没有登录系统就进入系统。*/
package com.towker.frame.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.towker.core.util.BeanUtils;
import com.towker.frame.share.rbac.res.PermissionShare;
import com.towker.frame.share.sys.user.UserShare;
import com.towker.web.MyRequest;

public class LoginFilter implements Filter {
	private String filterExt = "";
	private String excludeUrl = "";
	@SuppressWarnings("unused")
	private String validRights = "";
	private String notNogin = "";
	@SuppressWarnings("unused")
	private String noRights = "";
	private String contentType = "";

	public void init(FilterConfig filterConfig) throws ServletException {
		// 需要过滤的扩展名
		filterExt = filterConfig.getInitParameter("filterExt");
		// 不需要过滤的扩展名
		excludeUrl = filterConfig.getInitParameter("excludeUrl");
		// 是否验证权限
		validRights = filterConfig.getInitParameter("validRights");
		// 未登录跳转的页面
		notNogin = filterConfig.getInitParameter("notNogin");
		// 没有权限跳转的页面
		noRights = filterConfig.getInitParameter("noRights");
		// jsp页面内容的类型
		contentType = filterConfig.getInitParameter("contentType");
	}

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		req.setCharacterEncoding("utf-8");
		res.setContentType(contentType);
		MyRequest myRequest = new MyRequest(req);
		String url = myRequest.getRequestURI();
		// 判断是否是需要过滤的URL
		boolean isFilter = checkExt(url, filterExt);
		// 需要进行登录检查
		if (!isFilter) {
			filterChain.doFilter(req, res);
			return;
		}
		boolean isLogin = UserShare.isUserLogined(req);
		if (isLogin) {
			long userid = UserShare.getCurrentUserId(req);
			long sysid = UserShare.getCurrentSystemId(req);
			PermissionShare permissionHelper = (PermissionShare) BeanUtils
					.getBean(PermissionShare.class);
			boolean flag = permissionHelper.isPermission(userid, sysid, url);
			if (!flag)
				req.getRequestDispatcher(noRights).forward(request, response);
			else
				filterChain.doFilter(req, res);
		} else {
			if (checkExcludeUrl(url, excludeUrl))
				filterChain.doFilter(req, res);
			else {
				req.getRequestDispatcher(notNogin).forward(request, response);
			}
		}
	}

	/** 判断URL是不是以指定的扩展名结尾 */
	private boolean checkExt(String url, String ext) {
		url = url.toLowerCase();
		ext = ext.toLowerCase();
		String[] aryExt = ext.split(",");
		for (String str : aryExt)
			if (url.endsWith(str))
				return true;
		return false;
	}

	/** 判断URL是不是以不需要过滤的名称结尾的 */
	private boolean checkExcludeUrl(String url, String excludeUrl) {
		return checkExt(url, excludeUrl);
	}
}
